package com.guigu.order.controller;

import cn.hutool.core.map.MapUtil;
import com.guigu.order.entity.Order;
import com.guigu.order.service.OrderService;
import com.guigu.order.service.WeChatService;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName:WechatController
 * @Description://TODO
 * @Author:taoLong
 * @Date:2021/11/16:20:12 星期二
 * @email:2391611102@qq.com or 2391611102qq@gmail.com
 */
@Api(tags = "微信第三方管理")
@RestController
@RequestMapping("order/wechat-service")
public class WechatController {
    @Autowired
    private WeChatService weChatService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private OrderService orderService;

    /**
     * 生成微信二维码
     *
     * @param orderId 订单id
     * @return
     */
    @ApiOperation("生成微信二维码")
    @ApiImplicitParam(name = "orderId", value = "订单Id")
    @GetMapping("/createAQRCode/{orderId}/{orderNo}")
    public R<Map<String, Object>> createAQRCode(@PathVariable("orderId") String orderId, @PathVariable String orderNo) {
        Order order = orderService.getById(orderId);
        if (order.getPaymentStatus() == 2) {
            throw new TmsException(ResultCode.ERROR_CODE, "请勿重复支付!");
        }
        if (order.getPaymentStatus() == 3) {
            throw new TmsException(ResultCode.ERROR_CODE, "该订单已过期!");
        }
        Map<String, Object> aqrCode = weChatService.createAQRCode(orderId, orderNo);
        System.out.println("aqrCode = " + aqrCode);
        return new R<>(ResultCode.SUCCESS_CODE, aqrCode);
    }

    /**
     * 查看订单在支付时的状态
     *
     * @param orderId 订单状态
     * @return
     */
    @ApiOperation("查看支付状态")
    @ApiImplicitParam(name = "orderId", value = "订单Id")
    @GetMapping("/queryPayStatus/{orderId}/{orderNo}")
    public R queryPayStatus(@PathVariable String orderId, @PathVariable String orderNo) {
        Map<String, String> aqrCode = weChatService.queryPayStatus(orderId, orderNo);
        /**
         * 为null则代表支付失败!
         */
        if (MapUtil.isEmpty(aqrCode)) {
            return new R("2002", "支付失败!", false);
        }
        /**
         * 如果不为null,通过map获取订单状态
         */
        if (aqrCode.get("trade_state").equals("SUCCESS")) {
            R r = weChatService.dispatch(orderId);
            return r;
        }
        return new R("2500", "支付中", true);
    }


}
